//
//  OrderTimeViewController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/4/10.
//  Copyright (c) 2017 蓝布鲁. All rights reserved.
//

#import "OrderTimeViewController.h"
#import "UserInfoModel.h"
#import "ScheduleModel.h"
#import "MySlideBar.h"
#import "OrderTimeView.h"

@interface OrderTimeViewController ()
// 数据
@property (nonatomic, retain)NSMutableArray *scheduleModelArray;            // 数据数组

// 图像
@property (nonatomic, retain)MySlideBar *slideBar;
@property (nonatomic, retain)NSMutableArray *timeViewArray;

@end

@implementation OrderTimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择预约时间";
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = false;

    [self setupView];
    [self updateOrderTime];
}

#pragma mark - 自定义方法
-(void)setupView {
    // 设置顶部sliderBar
    self.slideBar = [[MySlideBar alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];



    self.slideBar.itemColor = [UIColor blackColor];
    self.slideBar.itemSelectedColor = [UIColor orangeColor];
    self.slideBar.sliderColor = [UIColor orangeColor];
    self.slideBar.itemScrollView.backgroundColor = [UIColor lightGrayColor];

    [self.view addSubview:self.slideBar];
}

#pragma mark - Private



-(void)updateOrderTime {
    self.scheduleModelArray = [[NSMutableArray alloc] init];
    self.timeViewArray = [[NSMutableArray alloc] init];

    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"/SDSY/TgetMyScheduling"
                                        parameters:@{@"tid": [UserInfoModel shareInstance].tid}
                                           success:^(NSDictionary *respondDictionary) {
                                               // 转化为模型储存
                                               for (NSDictionary *dictionary in respondDictionary[@"schule"]) {
                                                   ScheduleModel *model = [[ScheduleModel alloc] initWithDictionary:dictionary];
                                                   [self.scheduleModelArray addObject:model];
                                               }



                                               NSMutableArray *dayArray = [[NSMutableArray alloc] init];
                                               for (ScheduleModel *scheduleModel in self.scheduleModelArray) {
                                                   // 取出所有日期
                                                   [dayArray addObject:[scheduleModel.date formatDate]];

                                                   // 创建view
                                                   OrderTimeView *view = [[OrderTimeView alloc] init];
                                                   view.times = scheduleModel.times;
                                                   [self.timeViewArray addObject:view];
                                               }


                                               // 刷新sliderBar
                                               [self.slideBar setItemsTitle:dayArray pageViews:self.timeViewArray autoMargin:true];


                                           }
                                           failure:^(NSError *error) {
                                               NSLog(@"%@",error);
                                               [SVProgressHUD showfailed];
                                           }];
}

@end
